package com.meida.module.bus.provider.controller;

import com.meida.common.base.entity.EntityMap;
import com.meida.common.base.utils.FlymeUtils;
import com.meida.common.mybatis.model.ResultBody;
import com.meida.common.springmvc.base.BaseController;
import com.meida.common.utils.ApiAssert;
import com.meida.module.bus.client.entity.BusOrder;
import com.meida.module.push.provider.service.JPushService;
import com.meida.module.user.client.entity.AppUser;
import com.meida.module.user.provider.service.AppUserService;
import com.meida.msg.client.entity.MsgReceive;
import com.meida.msg.provider.service.MsgReceiveService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


/**
 * 消息接收表控制器
 *
 * @author flyme
 * @date 2020-02-28
 */
@Api(tags = "消息通知")
@RestController
@RequestMapping("/common/msg/")
public class MsgCommonController extends BaseController<MsgReceiveService, MsgReceive> {

    @Autowired
    private AppUserService appUserService;

    @Autowired
    private JPushService jPushService;

    @ApiOperation(value = "发送消息", notes = "发送消息")
    @PostMapping(value = "sendMsg")
    public ResultBody setReadState(@RequestParam(value = "userId") String userId, @RequestParam(value = "content") String content) {
        ApiAssert.isNotEmpty("userId不能为空", userId);
        ApiAssert.isNotEmpty("content不能为空", content);
        String msgTitle = "空位提醒";
        bizService.sendMsgReceiveNotice("EmptyPlace", Long.parseLong(userId), msgTitle, content);
        AppUser appUser = appUserService.getById(userId);
        Integer pushOtherMsg = FlymeUtils.getInteger(appUser.getPushOtherMsg(), 0);
        if (pushOtherMsg == 1) {
            EntityMap extra = new EntityMap();
            extra.put("msgType", BusOrder.class.getSimpleName());
            extra.put("msgUserId", userId);
            jPushService.sendByDeviceToken(userId, msgTitle, msgTitle, content, extra);
        }
        return ResultBody.ok("发送成功");
    }

}
